﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using Dtf.Core.Remoting;
using Dtf.Core.Resources;

namespace Dtf.Core
{
    public class App : RemotingObject
    {
        public App()
        {
        }

        public App(string path)
        {
            AppPath = path;
        }

        public virtual bool Start()
        {
            try
            {
                _process = Process.Start(_appPath);
            }
            catch (System.IO.FileNotFoundException)
            {
                return false;
            }
            return true;
        }

        public virtual bool Kill()
        {
            if (_process != null)
            {
                _process.Kill();
                return true;
            }
            else
            {
                Process[] ps = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(AppPath));
                foreach(Process p in ps)
                {
                    p.Kill();
                }
            }
            return false;
        }

        public bool AttachProcess(string processName)
        {
            Process[] procs = Process.GetProcessesByName(processName);
            if (procs.Length == 0)
            {
                return false;
            }
            _process = procs[0];
            return true;
        }
        public bool AttachProcess(int processId)
        {
            _process = Process.GetProcessById(processId);
            if (_process == null)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// //default timeout = 30000ms
        /// </summary>
        public virtual void WaitForInputIdle()
        {
            WaitForInputIdle(30000);
        }

        public virtual void WaitForInputIdle(int milliseconds)
        {
            int nRet = NativeMethods.WaitForInputIdle(_process.Handle, milliseconds);
            if (nRet != 0)
            {
                LogManager.Write("WaitForInputIdle is time out!", "red");
            }
        }

        public string ExtractString(string resourceId)
        {

            return _resMgr.ExtractString(resourceId);
            
        }


        public string AppPath
        {
            get
            {
                return _appPath;
            }
            set
            {
                _appPath = value;
                _resMgr = new Dtf.Core.Resources.ResourceManager(true);
                _resMgr.ResourceSearchPaths += "|" +Path.GetDirectoryName(value);
            }
        }

        public string AppFolder
        {
            get
            {
                return Path.GetDirectoryName(AppPath);
            }
        }


        private static Dtf.Core.Resources.ResourceManager _resMgr;
        private Process _process;
        private string _appPath;
        
    }
}
